﻿// ------------------------------------------------------------------
// FileName	：	DialogWindow.js
// Remark	：	弹出框
// ------------------------------------------------------------------
// VERSION		AUTHOR		DATE			CONTENT
// 1.0			席吉波	    2010-09-01	    创建
// ------------------------------------------------------------------

(function () {
    var ScriptName = "DialogWindow";
    function execute() {

        Type.registerNamespace('Kevin.Controls');

        $DialogWindow = Kevin.Controls.DialogWindow = function (element) {

            this._Url = null;
            this._UrlContent = null;
            this._PopUpBehaviorID = null;
            this._Control = null;
            this._ControlID = null;
            this._Width = 600;
            this._Height = 400;
            this._Title = null;
            this._Position = Sys.Extended.UI.PositioningMode.Center;
            this._ParentElementID = "";
            this._X = 0;
            this._Y = 0;
            this._DialogClassName = "dialog";

            this._DialogWindow = null; //PopUp a Modal Window form to query and select the code
            this._DialogWindowForm = null;
            this._DialogWindowForm_Input = null;
            this._DialogWindowForm_Button = null;
            this._DialogWindowForm_Close = null;
            this._DialogWindowForm_Title = null;
            this._alwaysNewContent = false;
            this._modeChangeAnimation = null;
            this._modeChangeMoveTopOrLeftAnimation = null;
            this._modeChangeMoveBottomOrRightAnimation = null;

            this._dialogResizeZoomInAnimation = null;
            this._dialogResizeZoomOutAnimation = null;

        }
        $DialogWindow.prototype = {

            initialize: function () {

                $DialogWindow.callBaseMethod(this, 'initialize');

                ControlClickHandler = Function.createDelegate(this, this.ControlClick);
                _DialogWindowForm_CloseClickHandler = Function.createDelegate(this, this._DialogWindowForm_CloseClick);
                _DialogWindowHiddenHandler = Function.createDelegate(this, this._DialogWindowHidden);
                var contorlIDArray = this._ControlID.split(',');
                var popupParentID = "";
                for (var index in contorlIDArray) {
                    var ctrlID = contorlIDArray[index];
                    var ctrl = null;
                    if (ctrlID) {
                        ctrl = $get(ctrlID);
                    }
                    if (ctrl) {
                        this._Control = ctrl; // $get(this._ControlID)
                        var location = $common.getLocation(ctrl);
                        this._X = location.x;
                        this._Y = location.y;
                        $addHandler(ctrl, "click", ControlClickHandler);
                        if (ctrl.tagName === "INPUT" && ctrl.type === "text") {
                            $addHandler(ctrl, "focus", ControlClickHandler);
                            $removeHandler(ctrl, "click", ControlClickHandler);
                        }
                    }
                }
                if (popupParentID == "") {
                    //popupParentID = contorlIDArray[0];
                }

                //this.get_element().style.display = 'none';
                // $common.setVisible(this.get_element(), false);

                this._BuildDialog();
                var windowID = this._ControlID + new Date().toLocaleTimeString() + Math.round(Math.random() * 10000) + 'DialogWindownPopupBehavior'; //

                this._DialogWindow = $create(Sys.Extended.UI.PopupBehavior,
                    { 'id': windowID, "positioningMode": this._Position, "parentElementID": this._ParentElementID }, null, null, this._DialogWindowForm);

                this._DialogWindow.add_hidden(_DialogWindowHiddenHandler);

                this._modeChangeMoveTopOrLeftAnimation = new Sys.Extended.UI.Animation.LengthAnimation(null, null, null, "style", null, 0, 0, "px");
                this._modeChangeMoveBottomOrRightAnimation = new Sys.Extended.UI.Animation.LengthAnimation(null, null, null, "style", null, 0, 0, "px");
                this._modeChangeAnimation = new Sys.Extended.UI.Animation.ParallelAnimation(null, .25, null, [this._modeChangeMoveTopOrLeftAnimation, this._modeChangeMoveBottomOrRightAnimation]);
                this._dialogResizeZoomInAnimation = new Sys.Extended.UI.Animation.ResizeAnimation(this._DialogWindowForm, 0.1, 25, this._Width, this.Height, "px")
                this._dialogResizeZoomOutAnimation = new Sys.Extended.UI.Animation.ResizeAnimation(this._DialogWindowForm, 0.1, 25, 0, 0, "px")
            },
            dispose: function () {
                $clearHandlers(this.get_element());
                $DialogWindow.callBaseMethod(this, 'dispose');
            },
            _BuildDialog: function () {

                var divID = "dialogWindow_" + this._Control.id;
                //*
                if ($get(divID)) {
                    $get(divID).parentNode.removeChild($get(divID));
                    //document.body.removeChild($get(divID));
                    // return;
                }
                //*/
                this._DialogWindowForm = $common.createElementFromTemplate({
                    nodeName: "div",
                    cssClasses: [this._DialogClassName],
                    properties: {
                        id: divID,
                        "display": "none"
                    }
                }, document.body);
                //                this._DialogWindowForm.style.width = this._Width; //"400px";
                //                this._DialogWindowForm.style.height = this._Height;
                if (isNaN(this._Width) || isNaN(this._Height)) {
                    this._Width = 600;
                    this._Height = 400;
                }
                $common.setSize(this._DialogWindowForm, { width: this._Width, height: this._Height });

                this._DialogWindowForm.style.zIndex = 10000;
                this._DialogWindowForm_Title = $common.createElementFromTemplate({
                    nodeName: "div",
                    cssClasses: ["title"],
                    properties: {
                        id: "dialog_title_" + divID
                    }
                }, this._DialogWindowForm);
                this._DialogWindowForm_Title.style.cursor = "move";

                var div_dialog_title_info = $common.createElementFromTemplate({
                    nodeName: "div",
                    cssClasses: ["info"],
                    properties: {
                        id: "div_dialog_title_info_" + divID
                    }
                }, this._DialogWindowForm_Title);
                div_dialog_title_info.innerHTML = this._Title;
                this._DialogWindowForm_Close = $common.createElementFromTemplate({
                    nodeName: "div",
                    cssClasses: ["ico"],
                    properties: {
                        id: "div_dialog_title_close_" + divID
                    }
                }, this._DialogWindowForm_Title);
                //div_dialog_title_close.innerHTML = "X";var div_dialog_title_close =
                var dialog_content = $common.createElementFromTemplate({
                    nodeName: "div",
                    cssClasses: ["content"],
                    properties: {
                        id: "dialog_content_" + divID

                    }
                }, this._DialogWindowForm);
                var contentHeight = parseInt(this._Height) - 43;
                var contentWidth = parseInt(this._Width);
                dialog_content.style.height = contentHeight + "px";

                this._divDataListInPopup = $common.createElementFromTemplate({
                    nodeName: "div",
                    cssClasses: ["loading"],
                    events: this._dialogTableCells$deleget,
                    properties: {
                        id: "div_dialog_conten" + divID
                    }
                }, dialog_content);
                //this._divDataListInPopup.innerHTML = this._UrlContent;


                //$addHandler(btnSearch, "click", _btnCodeSearchClickHandler);
                $addHandler(this._DialogWindowForm_Close, "click", _DialogWindowForm_CloseClickHandler);

                var ctID = divID + new Date().toLocaleTimeString() + Math.round(Math.random() * 10000) + "FloatingBehavior";
                $create(Sys.Extended.UI.FloatingBehavior, { "id": ctID, "handle": this._DialogWindowForm_Title }, null, null, this._DialogWindowForm);

                //      Sys.Extended.UI.FloatingBehavior
            },
            ControlClick: function () {
                this.Show();
            },
            Show: function () {
                if (this._DialogWindow != null && this._DialogWindowForm != null) {
                    this._DialogWindowForm.style.zIndex = 10000;

                    this._DialogWindowForm.style.display = '';

                    if (this._Url == '')
                        return;


                    var INPUT = $get('DialogWindowIDByKevinXi');
                    if (INPUT) {
                        INPUT.value = this._Control.id; //'dialogWindow_' + this._Control.id;
                    }
                    else {
                        $common.createElementFromTemplate({
                            nodeName: 'input',
                            properties: {
                                type: 'hidden',
                                id: 'DialogWindowIDByKevinXi',
                                value: 'dialogWindow_' + this._Control.id
                            }
                        }, document.body);
                    }
                    var height = parseInt(this._Height) - 50;
                    var width = parseInt(this._Width) - 20;

                    if (this._clientStateField) {
                        var clientState = Sys.Serialization.JavaScriptSerializer.deserialize(this._clientStateField.value);

                        var strUrl = clientState.URL || this._Url;
                        this._Url = strUrl; // clientState.URL;

                        if (clientState.ClientQueryMethods && clientState.ClientQueryMethods.length > 0) {

                            var paras = '';

                            for (var index in clientState.ClientQueryMethods) {
                                var id = clientState.ClientQueryMethods[index].ClientID;
                                var method = clientState.ClientQueryMethods[index].Method;
                                if (method == "") {
                                    paras += "\"" + $get(id).value + "\"" + ',';
                                }
                                else {
                                    paras += "\"" + eval(method) + "\"" + ',';
                                }
                            }
                            paras = paras.substring(0, paras.length - 1);
                            strUrl = "String.format(\"" + strUrl + "\"," + paras + ")";
                            strUrl = eval(strUrl);
                            this._Url = strUrl;
                        }
                    }
                    if (this._divDataListInPopup.innerHTML == "" || this._alwaysNewContent) {
                        var IFrame = '<iframe frameborder="0" style="position:absolute;" height="' + height + '" width="' + width + '" name="IFrame' + this._Control.id + '" id="IFrame' + this._Control.id + '" src="' + this._Url + '"/>';
                        //                    var FORM = "<form method=\"post\" action=\"" + this._Url + "\" target=\"IFrame" + this._Control.id + "\">";
                        //                    //"dialogWindow_" + this._Control.id 为弹出窗体的ID
                        //                    //为了在弹出的页面能将此该窗体关闭，特在此新增该按钮
                        //                    FORM += '<input type="hidden" id="DialogWindowIDByKevinXi" value="' + "dialogWindow_" + this._Control.id + '" />';
                        //                    FORM += '</form>';
                        //                    FORM.appendChild(INPUTID).appendChild(FORMEnd);
                        //                    IFrame.appendChild(FORM);
                        //FORM.submit();
                        //IFrame.src=this._Url;
                        //                    if (IFrame.attachEvent) {
                        //                        IFrame.attachEvent("onload", function() {
                        //                            this._divDataListInPopup.innerHTML = IFrame;
                        //                        });
                        //                    } else {
                        //                        IFrame.onload = function() {
                        //                            this._divDataListInPopup.appendChild(IFrame);
                        //                        };
                        //                    }
                        //
                        //IFrame.forms[0].appendChild(inputhidden);
                        this._divDataListInPopup.innerHTML = IFrame;
                    }
                    this._DialogWindow.show();
                }
            },
            _DialogWindowForm_CloseClick: function () {
                //this._dialogResizeZoomOutAnimation.play();
                this._DialogWindow.hide();
            },
            _DialogWindowHidden: function () {

                this.raiseHidden(Sys.EventArgs.Empty);
            },
            raiseHidden: function (eventArgs) {

                var handler = this.get_events().getHandler('hidden');
                if (handler) {
                    handler(this, eventArgs);
                }
            },
            saveClientState: function () {

            },
            loadClientState: function () {
                if (this._clientStateField.value == '' || this._clientStateField.value == 'undefined') {
                    return;
                }
                this._clientState = Sys.Serialization.JavaScriptSerializer.deserialize(this._clientStateField.value);
                //this._clientState = this._clientStateField.value;
            },
            get_clientStateField: function () {
                return this._clientStateField;
            },
            set_clientStateField: function (value) {
                if (this._clientStateField != value) {
                    this._clientStateField = value;
                    this.raisePropertyChanged('clientStateField');
                }
            },
            get_Url: function () {
                return this._Url;
            },
            set_Url: function (value) {
                this._Url = value;
            },
            get_UrlContent: function () {
                return this._UrlContent;
            },
            set_UrlContent: function (value) {
                this._UrlContent = value;
            },
            get_ControlID: function () {
                return this._ControlID;
            },
            set_ControlID: function (value) {
                this._ControlID = value;
            },
            get_PopUpBehaviorID: function () {
                return this._PopUpBehaviorID;
            },
            set_PopUpBehaviorID: function (value) {
                this._PopUpBehaviorID = value;
            },
            get_Title: function () {
                return this._Title;
            },
            set_Title: function (value) {
                this._Title = value;
            },
            get_DialogClassName: function () {
                return this._DialogClassName;
            },
            set_DialogClassName: function (value) {
                this._DialogClassName = value;
            },
            get_Width: function () {
                return this._Width;
            },
            set_Width: function (value) {
                this._Width = value;
            },
            get_Height: function () {
                return this._Height;
            },
            set_Height: function (value) {
                this._Height = value;
            },
            get_Position: function () {
                return this._Position;
            },
            set_Position: function (value) {
                this._Position = value;
            },
            get_ParentElementID: function () {
                return this._ParentElementID;
            },
            set_ParentElementID: function (value) {
                this._ParentElementID = value;
            },
            get_AlwaysNewContent: function () {
                return this._alwaysNewContent;
            },
            set_AlwaysNewContent: function (value) {
                this._alwaysNewContent = value;
            }

        }//end prototype
        $DialogWindow.registerClass('Kevin.Controls.DialogWindow', Sys.UI.Control, Sys.IDisposable);

    } //end execute

    if (window.Sys && Sys.loader) {
        Sys.loader.registerScript(ScriptName, ["DialogWindow"], null, execute);

    }
    else {
        execute();
    }

})()//end